package com.xujl.fastlib.utils;

import java.math.BigDecimal;

/**
 * @author x
 * @Description
 */
public class NumberUtils {
    public static float floatOf (String num) {
        if (StringUtils.isEmpty(num)) {
            return 0f;
        }
        float result = 0f;
        try {
            result = Float.valueOf(num);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static double doubleOf (String num) {
        if (StringUtils.isEmpty(num)) {
            return 0;
        }
        double result = 0f;
        try {
            result = Double.valueOf(num);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static long longOf (String num) {
        if (StringUtils.isEmpty(num)) {
            return 0L;
        }
        long result = 0L;
        try {
            result = (long) doubleOf(num);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int intOf (String num) {
        if (StringUtils.isEmpty(num)) {
            return 0;
        }
        int result = 0;
        try {
            result = (int) doubleOf(num);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static BigDecimal bigDecimalOf (String str) {
        if (StringUtils.isEmpty(str)) {
            return new BigDecimal("0");
        }
        try {
            return new BigDecimal(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BigDecimal("0");
    }

    public static BigDecimal bigDecimalOf (double num) {
        try {
            return new BigDecimal(num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BigDecimal(0);
    }

    public static BigDecimal bigDecimalOf (float num) {
        try {
            return new BigDecimal(num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BigDecimal(0);
    }

    public static BigDecimal bigDecimalOf (int num) {
        try {
            return new BigDecimal(num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BigDecimal(0);
    }
}

